from django.shortcuts import render
from timeholder import models
from timeholder.models import Task
from datetime import datetime

def test(request):
    return render(request,'test.html',{'list':[1,2,4,5]})


def clock(request):
    countdown =1500
    context = {
        'countdown': countdown
    }
    return render(request, 'clock.html', context)
from timeholder import models
from datetime import  datetime,timedelta
def data(request):
    # 用于调试的临时数据
    cur_user='user1'
    cur_date=datetime(2017,11,5).date()
    print(cur_date)
    # 获取该用户的所有活动
    task_obj=models.Task.objects.filter(username=cur_user)
    #获取该用户的所有每日总结
    dateDesc_obj=models.DateDesc.objects.filter(username=cur_user)
    DayDes="今天没有总结哦"
    #获取该用的在cur_date下的每日总结
    for date_desc in dateDesc_obj:
        if(date_desc.date==cur_date):
            DayDes=date_desc.des
            break
    # 获取该用户的当天活动
    cur_date_task_names,cur_date_task_times=[],[]
    for i in range(len(task_obj)):
        if  task_obj[i].start_datetime.date()==cur_date:
            cur_date_task_names.append(task_obj[i].name)
            cur_date_task_times.append(25)
    #为了正确传递参数，将活动名封装为字符串
    totalName=cur_date_task_names[0]
    for i in range(len(cur_date_task_names)-1):
        totalName+=","
        totalName+=cur_date_task_names[i+1]
    #统计从当天开始，向前推七天，每天的学习时间：
    weekTime=[]
    for i in range(7):
        this_date=cur_date-timedelta(days=i)
        weekTime.append(len([task for task in task_obj if task.start_datetime.date()==this_date])*25)
    weekTime=list(reversed(weekTime))
    #统计实现
    return render(request,'data.html',{"dayDes":DayDes,'day24TasksName':totalName,'day24TasksTime':cur_date_task_times,'weekTime':weekTime})


